<?php

/**
 * Created by PhpStorm.
 * User: Asha
 * Date: 2016/11/4
 * Time: 10:58
 */
class Version_model extends CI_Model{


    public function __construct()
    {
        parent::__construct();
       // $this->load->database();
    }

    /**
     * desc: 获取所有产品最新版本下载信息
     * @param $type
     * @return array
     */
    public function newVersion()
    {
        $re = array('result' => 1);
        $ref = array('result' => 0);
        try {
            $products = $this->getProTypes();
            if (!empty($products)) {
                foreach ($products as $val) {
                    $prokey = $val['pro_name'];
                    $re['list'][$prokey] = $this->newVersionOne($prokey);
                }
            }
            return $re;

        }catch(Exception $ex){ echo 'error'; exit;
           return $ref;
        }

    }

    /**
     * desc: 获取一个产品的 最新版本下载信息
     * @param $type
     * @return array
     */
	public function newVersionOne($type){

        try {
		    $sql = "SELECT `modify_date`,`size`,`version_no` as `version`,`desc`,`download_addr` as `url` FROM ".TAB_VERSIONINFO." WHERE pro_name = '".$type."' order by create_date desc limit 1";
            //echo $sql; exit;
            $result = $this -> db -> query($sql) -> result('array');
            // $err = $this->db->_error_message(); var_dump($err); exit;
            //$errinfo = $this->db->error(); no run

            return $result;
        }catch(Exception $ex){ echo 'error'; exit;

        }
	}

    /**
     * desc: 获取当前产品的最新版本信息
     * @param $type
     * @param $version
     */
    public function checkverion($type,$version){
        $ref = array('result'=>1);
        $newinfo = $this->newVersionOne($type);
        $newv = substr($newinfo[0]['version'],1);
        if($version != $newv){
            $ref['version'] = $newv;
            $ref['url'] = $newinfo[0]['url'];
        }else{
            $ref['version'] = '';
            $ref['url'] = '';
        }
        return $ref;

    }

    /**
     * 获取数据库里，当前产品类型
     */
    public function getProTypes(){

        $sql = "SELECT distinct `pro_name` FROM `version_info` WHERE `pro_name` != 'kisupload'";
        $result = $this -> db -> query($sql) -> result('array');
        return $result;
    }

    /**
     * desc:版本更新 历史记录
     * @param $type
     * @return array
     */
    public function historyAll($type){

        $ref = array('result'=>0);
        try{
            $re = array('result'=>1);
            /*             $products = $this->getProTypes();
                       if(!empty($products)){
                            foreach($products as $val){
                                $prokey = $val['pro_name'];
                                $re['list'][$prokey] = $this->historyOne($prokey);
                            }
                        }*/
            $sql = "SELECT `desc`,`version_no`,`modify_date`,`modify_content` FROM ".TAB_VERSIONINFO." WHERE `pro_name` != 'kisupload' order by modify_date desc";
            $result = $this -> db -> query($sql) -> result('array');//var_dump($result); exit;
            $re['list'] = $result;
            return $re;

        }catch(\Exception $e){
            return $ref;
        }

    }

    /**
     * desc:版本更新 某个产品返回
     * @param $type
     * @return array
     */
    public function history($type){

        $ref = array('result'=>0);
        try {
            $re = array('result' => 1);
            //$sql = "SELECT `desc`,`version_no`,`modify_date`,`modify_content` FROM `version_info` WHERE pro_name='".$type."' order by modify_date desc";
            //$result = $this -> db -> query($sql) -> result('array');
            $result = $this->historyOne($type);
            //var_dump($result);

            if($result != 'error'){
                if ($result || !empty($result)) {
                    $re['list'] = $result;
                    return $re;
                } else {
                    return $ref;
                }
            }
        }catch(\Exception $e){ //echo 'catch error<br>';  var_dump($e->getMessage()); exit;
            return $ref;
        }


    }

    /**
     * desc:版本更新历史记录 某个产品
     * @param $type
     * @return array
     */
    public function historyOne($type){

       // $ref = array('result'=>0);
        try{

           // $re = array('result'=>1);
            $sql = "SELECT `desc`,`version_no`,`modify_date`,`modify_content` FROM ".TAB_VERSIONINFO."  WHERE pro_name='".$type."' order by modify_date desc";
            $result = $this -> db -> query($sql) -> result('array');//var_dump($result); exit;
            return $result;

/*            if($result || !empty($result)){
                $re['list']=$result;
                return $re;
            }else{
                return $ref;
            }*/
        }catch(\Exception $e){ //echo 'catch error<br>';  var_dump($e->getMessage()); exit;
            return 'error'; // exception'.$sql;
        }
       /* finally{ //echo 'kkkkkerror'; exit;
            return 'error exception db';
        }*/

    }

    /**
     * desc: 获取web端首页的最新版本的下载连接
     */
    public function downlinks(){
        $re = array('result' => 1);
        $ref = array('result' => 0);
        try {
            $products = $this->getProTypes();
            if (!empty($products)) {
                foreach ($products as $val) {
                    $prokey = $val['pro_name'];
                    //$re['list'][$prokey] = $this->newVersionOne($prokey);
                    $sql = "SELECT `download_addr` FROM ".TAB_VERSIONINFO."  WHERE pro_name='".$prokey."' order by modify_date desc limit 1";
                    $result = $this -> db -> query($sql) -> result('array');
                    $rkey = $prokey.'url';
                    $re[$rkey] = $result[0]['download_addr'];
                }
            }
            return $re;

        }catch(Exception $ex){ echo 'error'; exit;
            return $ref;
        }
    }

    /**
     * desc: 获取web端首页的最新版本的下载连接
     */
    public function downAndroid(){

        $sql = "SELECT `download_addr`,`version_no` FROM ".TAB_VERSIONINFO."  WHERE pro_name='android' order by modify_date desc limit 1";
        $result = $this -> db -> query($sql) -> result('array');
       // return $result[0]['download_addr'];
        return $result[0]['version_no'];

    }

    public function downKisupload(){
        $sql = "SELECT `download_addr`,`version_no`,`pro_name` FROM ".TAB_VERSIONINFO."  WHERE pro_name='kisupload' order by modify_date desc limit 1";
        $result = $this -> db -> query($sql) -> result('array');
        // return $result[0]['download_addr'];
        //return $result[0]['version_no'];
        return $result;

    }


    public function saveVersionInfo($data){
        $this->db->insert(TAB_VERSIONINFO, $data);
        return $this->db->insert_id();
    }
	
	public function updateKisUploader($savedata, $pname){
        
        $this->db->where('pro_name', $pname);
        $this->db->update(TAB_VERSIONINFO, $savedata);
        return $this;
	}



}